//
//  SupplierChooseVC.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2020/7/29.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "SupplierChooseVC.h"
#import "SupplierChooseCell.h"
#import "SupplierMainModel.h"
#import "NSString+PinYin.h"

@interface SupplierChooseVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UITextField *searchField;
@property (nonatomic, strong) UIButton *searchBtn;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *array;

@end

@implementation SupplierChooseVC

-(UIView *)topView {
    if (!_topView) {
        _topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 50)];
        _topView.backgroundColor = HexColor(0xffffff);
        
        self.searchField = [[UITextField alloc] initWithFrame:CGRectMake(15, 7, ScreenW - 67 - 15, 36)];
        self.searchField.backgroundColor = HexColor(0xF2F2F5);
        self.searchField.layer.masksToBounds = YES;
        self.searchField.layer.cornerRadius = 6;
        self.searchField.placeholder = @"输入供应商名称或编号";
        self.searchField.font = [UIFont systemFontOfSize:16];
        self.searchField.textColor = HexColor(0x333333);
        UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 36)];
        leftView.backgroundColor = HexColor(0xF2F2F5);
        self.searchField.leftView = leftView;
        self.searchField.leftViewMode = UITextFieldViewModeAlways;
        [_topView addSubview:self.searchField];
        
        self.searchBtn = [[UIButton alloc] initWithFrame:CGRectMake(ScreenW - 67, 7, 67, 36)];
        [self.searchBtn setTitle:@"搜索" forState:0];
        [self.searchBtn setTitleColor:Color_ThemeColor forState:0];
        self.searchBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        [self.searchBtn addTarget:self action:@selector(searchClick) forControlEvents:UIControlEventTouchUpInside];
        [_topView addSubview:self.searchBtn];
        
        UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(0, 49, ScreenW, 1)];
        line.backgroundColor = HexColor(0xF2F2F5);
        [_topView addSubview:line];
    }
    return _topView;
}

- (void)searchClick {
    [self requestForData];
}

-(NSMutableArray *)array {
    if (!_array) {
        _array = [NSMutableArray array];
    }
    return _array;
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.topView.nim_bottom, ScreenW, ScreenH - NaviHeight - BottomHeight - self.topView.nim_height)];
        _tableView.backgroundColor = HexColor(0xffffff);
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        __weak_self;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [weakself requestForData];
        }];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"选择供应商";
    [self.view addSubview:self.topView];
    [self.view addSubview:self.tableView];
    [self requestForData];
}

- (void)requestForData {
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    if (self.searchField.text.length) {
        [dic setObject:self.searchField.text forKey:@"keyword"];
    }
    [HttpRequest GetWithApi:ReceiptDeliverySupplierList params:dic progress:^(NSProgress * _Nonnull progress) {

    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            NSArray *arr = [SupplierMainModel mj_objectArrayWithKeyValuesArray:[responseObject objectForKey:@"list"]];
            self.array = [[arr arrayModelWithPinYinFirstLetterFormat] mutableCopy];
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
        }
    } fail:^(NSError * _Nonnull error) {

    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.array.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSDictionary *dict = self.array[section];
    NSMutableArray *array = dict[@"content"];
    return [array count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dict = self.array[indexPath.section];
    NSMutableArray *array = dict[@"content"];
    SupplierChooseCell *cell = [SupplierChooseCell SupplierChooseCellWithTableView:tableView];
    cell.model = array[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dict = self.array[indexPath.section];
    NSMutableArray *array = dict[@"content"];
    SupplierChooseCell *cell = [SupplierChooseCell SupplierChooseCellWithTableView:tableView];
    cell.model = array[indexPath.row];
    return cell.height;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dict = self.array[indexPath.section];
    NSMutableArray *array = dict[@"content"];
    if (self.finishBlock) {
        self.finishBlock(array[indexPath.row]);
    }
    [self.rt_navigationController popViewControllerAnimated:YES];
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    NSMutableArray *resultArray = [NSMutableArray array];
    for (NSDictionary *dict in self.array) {
        NSString *title = dict[@"firstLetter"];
        [resultArray addObject:title];
    }
    return resultArray;
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    //这里是为了指定索引index对应的是哪个section的，默认的话直接返回index就好。其他需要定制的就针对性处理

    //以下e.g.实现了将索引0对应到section 1里。其他的正常对应。

//    if (index == 0) {
//        return 1;
//    }
//    return index;

    return index;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
